package com.lzy.business.controller;

import com.lzy.framework.common.request.BaseRequest;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DateUtil;
import com.lzy.framework.core.utils.FtpUtil;
import com.lzy.framework.core.utils.ImageUtils;
import com.lzy.framework.core.utils.QiniuUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by ellengou on 2017/4/21 0021.
 * since 2.0
 */
@Controller
@RequestMapping("/upload")
public class UploadController {
    private static final Logger LOGGER = LoggerFactory.getLogger(UploadController.class);
    @RequestMapping(value = "/image", method = RequestMethod.POST)
    @ResponseBody
    public String uploadTestImage(@RequestParam(value = "image", required = false) MultipartFile file) {
        Ensure.that(file).isNotNull("00000008");
        String path = QiniuUtils.upload(file, QiniuUtils.QINIU_BUCKET_DOMAIN_ENUM.TEST);
        return path;
    }

    @RequestMapping(value = "/goods", method = RequestMethod.POST)
    @ResponseBody
    public String uploadGoodsImage(@RequestParam(value = "image", required = false) MultipartFile file) {
        Ensure.that(file).isNotNull("00000008");
        String path = QiniuUtils.upload(file, QiniuUtils.QINIU_BUCKET_DOMAIN_ENUM.GOODS);
        return path;
    }

    @RequestMapping(value = "/icon", method = RequestMethod.POST)
    @ResponseBody
    public String uploadIcon(@RequestParam(value = "image", required = false) MultipartFile file) {
        Ensure.that(file).isNotNull("00000008");
        String path = QiniuUtils.upload(file, QiniuUtils.QINIU_BUCKET_DOMAIN_ENUM.ICON);
        return path;
    }

    @RequestMapping(value = "/order", method = RequestMethod.POST)
    @ResponseBody
    public String uploadOrder(@RequestParam(value = "image", required = false) MultipartFile file) {
        Ensure.that(file).isNotNull("00000008");
        String path = QiniuUtils.upload(file, QiniuUtils.QINIU_BUCKET_DOMAIN_ENUM.ORDER);
        return path;
    }

    @RequestMapping(value = "/base", method = RequestMethod.POST)
    @ResponseBody
    public String uploadBase(@RequestParam(value = "image", required = false) MultipartFile file) {
        Ensure.that(file).isNotNull("00000008");
        String path ="";
        try {
            path =  FtpUtil.uploadFile(file.getOriginalFilename(),file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return path;
    }

    @RequestMapping(value = "/office", method = RequestMethod.POST)
    @ResponseBody
    public String uploadOffice(@RequestParam(value = "image", required = false) MultipartFile file) {
        Ensure.that(file).isNotNull("00000008");
        String path ="";
        try {
            path = QiniuUtils.uploadFile(file.getInputStream(), QiniuUtils.QINIU_BUCKET_DOMAIN_ENUM.BASE, DateUtil.getCurrentMillisecond() + "." + QiniuUtils.parseSuffix(file.getOriginalFilename()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return path;
    }

    @RequestMapping(value = "/video", method = RequestMethod.POST)
    @ResponseBody
    public String uploadVideo(@RequestParam(value = "video", required = false) MultipartFile file) {
        Ensure.that(file).isNotNull("00000008");
        String path ="";
        try {
            path = QiniuUtils.uploadFile(file.getInputStream(), QiniuUtils.QINIU_BUCKET_DOMAIN_ENUM.VIDEO, DateUtil.getCurrentMillisecond() + "." + QiniuUtils.parseSuffix(file.getOriginalFilename()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return path;
    }

    @RequestMapping(value = "/office-base", method = RequestMethod.POST)
    @ResponseBody
    public String uploadOffice2Base(@RequestParam(value = "image", required = false) MultipartFile file) {
        Ensure.that(file).isNotNull("00000008");
        String path ="";
        try {
            path = QiniuUtils.uploadFile(file.getInputStream(), QiniuUtils.QINIU_BUCKET_DOMAIN_ENUM.TEST, DateUtil.getCurrentMillisecond() + "." + QiniuUtils.parseSuffix(file.getOriginalFilename()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return path;
    }

    @RequestMapping(value = "/test", method = RequestMethod.POST)
    @ResponseBody
    public String testZipImage(String img) {
//        Ensure.that(file).isNotNull("00000008");
        String path = null;
        String imagePath = ImageUtils.base64StringToImage(img);
        if (img !=null) {
            try {
                InputStream input = new FileInputStream(imagePath);
                path =  QiniuUtils.upload(input, QiniuUtils.QINIU_BUCKET_DOMAIN_ENUM.ICON);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return path;
    }

    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    @ResponseBody
    public Result deleteImage(@RequestBody BaseRequest file) {
        Ensure.that(file).isNotNull("00000010");
        String fileName = file.getName();
        if (fileName.contains("http://") || fileName.contains("https://")) {
            if (fileName.contains("?"))
                fileName = fileName.substring(0, fileName.lastIndexOf("?"));
            fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
        }
        try {
            QiniuUtils.delete(QiniuUtils.QINIU_GOODS_BUCKET, fileName);
        } catch (Exception e) {
            e.printStackTrace();
            Ensure.that(e).isNull("00000011");
        }
        return new Result();
    }

    @RequestMapping(value = "/activity", method = RequestMethod.POST)
    @ResponseBody
    public String uploadActivity(@RequestParam(value = "image", required = false) MultipartFile file) {
        Ensure.that(file).isNotNull("00000008");
        String path = QiniuUtils.upload(file, QiniuUtils.QINIU_BUCKET_DOMAIN_ENUM.ACTIVITY);
        return path;
    }

}